import { createRouter, createWebHashHistory } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import cache from "@/utils/cache";
import { firstMenu } from "@/utils/map-menus";
NProgress.configure({
  easing: "speed", // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: false, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3, // 初始化时的最小百分比
});
const routes = [
  {
    path: "/",
    redirect: "/main",
  },
  {
    path: "/login",
    redirect: "/login/logining",
  },
  {
    path: "/main",
    name: "main",
    component: () => import("@/views/main/main.vue"),
    children: [
      {
        path: "system/bookSystem",
        name: "bookSystem",
        component: () =>
          import("@/views/main/system/bookSystem/bookSystem.vue"),
        children: [],
      },
      {
        path: "system/user",
        name: "user",
        component: () => import("@/views/main/system/user/user.vue"),
        children: [],
      },
      {
        path: "product/goods",
        name: "goods",
        component: () => import("@/views/main/product/goods/goods.vue"),
        children: [],
      },
      {
        path: "analysis/dashboard",
        name: "dashboard",
        component: () =>
          import("@/views/main/analysis/dashboard/dashboard.vue"),
        children: [],
      },
      {
        path: "analysis/overview",
        name: "overview",
        component: () => import("@/views/main/analysis/overview/overview.vue"),
        children: [],
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/login.vue"),
    children: [
      {
        path: "logining",
        name: "logining",
        component: () => import("@/views/login/cpns/login-panel.vue"),
      },
      {
        path: "register",
        name: "register",
        component: () => import("@/views/register/register.vue"),
      },
    ],
  },
  {
    path: "/:pathMatch(.*)*",
    name: "notFound",
    component: () => import("@/views/not-found/not-found.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach(async (to) => {
  if (to.path !== "/login/logining" && to.path !== "/login/register") {
    const token = cache.getCache("token");
    if (!token) {
      return "/login/logining";
    }
  }
  if (to.path === "/main") {
    return firstMenu.url;
  }
  NProgress.start();
});
router.afterEach(async () => {
  NProgress.done();
});
export default router;
